package com.dashan.backend.datastore;

import java.util.Date;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class DsFriendInvitation {
    @SuppressWarnings("unused")
    @PrimaryKey
    @Persistent
    private String key;
    
    @Persistent
    private String invitationFromUserName;
    
    @Persistent
    private String invitationToUserName;
    
    @Persistent
    private String message;
    
    @Persistent
    private Date timestamp;
    
    public DsFriendInvitation(String invitationFromUserName,
            String invitationToUserName, String message, Date timestamp) {
        this.invitationFromUserName = invitationFromUserName;
        this.invitationToUserName = invitationToUserName;
        this.message = message;
        this.timestamp = timestamp;
        this.key = buildKey(invitationFromUserName, invitationToUserName);
    }

    public String getInvitationFromUserName() {
        return invitationFromUserName;
    }

    public String getInvitationToUserName() {
        return invitationToUserName;
    }

    public String getMessage() {
        return message;
    }

    public Date getTimestamp() {
        return timestamp;
    }
    
    public Element getXmlElement(Document xmlDoc) {
        Element root = xmlDoc.createElement("request");
        if (getTimestamp() != null) {
            Element ele = null;    
            ele = xmlDoc.createElement("from");
            ele.appendChild(xmlDoc.createTextNode(getInvitationFromUserName()));
            root.appendChild(ele);
            ele = xmlDoc.createElement("to");
            ele.appendChild(xmlDoc.createTextNode(getInvitationToUserName()));
            root.appendChild(ele);
            ele = xmlDoc.createElement("message");
            ele.appendChild(xmlDoc.createTextNode(getMessage()));
            root.appendChild(ele);
            ele = xmlDoc.createElement("timestamp");
            ele.appendChild(xmlDoc.createTextNode(getTimestamp().toString()));
            root.appendChild(ele);
        }
        return root;
    }
    
    public static String buildKey(String from, String to) {
        return from + ">" + to;
    }
}
